package com.huike.result;

import com.huike.constant.HttpStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 封装分页查询结果
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MyPageResult<T> implements Serializable {
    /** 成功 */
    public static final int SUCCESS = HttpStatus.SUCCESS;
    /** 失败 */
    public static final int FAIL = HttpStatus.ERROR;
    private int code;
    private String msg;
    private long total; //总记录数
    private List<T> rows; //当前页数据集合
    public static <T> MyPageResult<T> ok(List<T> rows, long total)
    {
        return restResult(rows,total, SUCCESS, "操作成功");
    }

    public static <T> MyPageResult<T> ok(List<T> rows, long total, String msg)
    {
        return restResult(rows,total, SUCCESS, msg);
    }
    public static <T> MyPageResult<T> fail(List<T> rows, long total)
    {
        return restResult(rows,total, FAIL, "操作成功");
    }

    public static <T> MyPageResult<T> fail(List<T> rows, long total, String msg)
    {
        return restResult(rows,total, FAIL, msg);
    }

    private static <T> MyPageResult<T> restResult(List<T> rows ,long total, int code, String msg)
    {
        MyPageResult<T> apiResult = new MyPageResult<>();
        apiResult.setCode(code);
        apiResult.setRows(rows);
        apiResult.setTotal(total);
        apiResult.setMsg(msg);
        return apiResult;
    }
    public static <T> Boolean isError(MyPageResult<T> ret)
    {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(MyPageResult<T> ret)
    {
        return MyPageResult.SUCCESS == ret.getCode();
    }
}
